package com.bkgy.controller.technology.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.controller.repTechFit.entity.RepTechFit;
import com.bkgy.controller.repTechFit.service.IRepTechFitService;
import com.bkgy.controller.tSolutionPdo.entity.TSolutionPdo;
import com.bkgy.controller.tSolutionPdo.service.ITSolutionPdoService;
import com.bkgy.controller.technology.entity.TTechTcm2280Param;
import com.bkgy.controller.technology.entity.Tcm2280Base;
import com.bkgy.controller.technology.entity.Tcm2280ProcessDatas;
import com.bkgy.controller.technology.entity.dto.*;
import com.bkgy.controller.technology.mapper.ITcmProcessMapper;
import com.bkgy.controller.technology.service.ITcm2280ProcessService;
import com.bkgy.controller.technology.service.ITechTcmParamService;
import com.bkgy.modular.datacollect_bx.bean.JDataSavemong2280_1;
import com.bkgy.sys.common.constant.CommonConstant;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.DateUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

@Slf4j
@Service
@RequiredArgsConstructor
public class Tcm2280ProcessServiceImpl extends ServiceImpl<ITcmProcessMapper, Tcm2280ProcessDatas> implements ITcm2280ProcessService {
    private final ITcmProcessMapper mapper;
    private final IRepTechFitService techFitService;
    private final ITechTcmParamService paramService;
    private final ITSolutionPdoService solutionService;
    private final MongoTemplate mongoTemplate;

    @Override
    public ResultData<TechTcm2280CharRes> selectTechFit(TechChartDtoReq tc) {
        TechTcm2280CharRes obj = new TechTcm2280CharRes();
        obj.setExtIdArr(new ArrayList<>());
        obj.setCredateArr(new ArrayList<>());
        obj.setTcmProcessSpeedArr(new ArrayList<>());
        obj.setTcmTank1TempArr(new ArrayList<>());
        obj.setTcmTank2TempArr(new ArrayList<>());
        obj.setTcmTank3TempArr(new ArrayList<>());
        obj.setAcid1(new ArrayList<>());
        obj.setFeContent1(new ArrayList<>());
        obj.setAcid3(new ArrayList<>());
        obj.setFeContent3(new ArrayList<>());
        obj.setTcmRinsePhArr(new ArrayList<>());
        obj.setTcmDryTempArr(new ArrayList<>());
        obj.setRollSpeedArr(new ArrayList<>());
        obj.setTcmTank1EmuTempArr(new ArrayList<>());
        obj.setTcmTank2EmuTempArr(new ArrayList<>());
        obj.setTcmTank3EmuTempArr(new ArrayList<>());
        obj.setTcm1RuPressureArr(new ArrayList<>());
        obj.setTcm2RuPressureArr(new ArrayList<>());
        obj.setTcm3RuPressureArr(new ArrayList<>());
        obj.setTcm4RuPressureArr(new ArrayList<>());
        obj.setTcm5RuPressureArr(new ArrayList<>());

        LambdaQueryWrapper<Tcm2280ProcessDatas> w = buildTcm2280Wrapper(tc);
        w.select(Tcm2280ProcessDatas::getExtid,
                Tcm2280ProcessDatas::getEndtime,
                Tcm2280ProcessDatas::getTcmProcessSpeedAvg,
                Tcm2280ProcessDatas::getTcmTank1TempAvg,
                Tcm2280ProcessDatas::getTcmTank2TempAvg,
                Tcm2280ProcessDatas::getTcmTank3TempAvg,
                Tcm2280ProcessDatas::getAcid1,
                Tcm2280ProcessDatas::getFeContent1,
                Tcm2280ProcessDatas::getAcid3,
                Tcm2280ProcessDatas::getFeContent3,
                Tcm2280ProcessDatas::getTcmRinsePhAvg,
                Tcm2280ProcessDatas::getTcmDryTempAvg,
                Tcm2280ProcessDatas::getRollSpeedAvg,
                Tcm2280ProcessDatas::getTcmTank1EmuTempAvg,
                Tcm2280ProcessDatas::getTcmTank2EmuTempAvg,
                Tcm2280ProcessDatas::getTcmTank3EmuTempAvg,
                Tcm2280ProcessDatas::getTcm1RuPressureAvg,
                Tcm2280ProcessDatas::getTcm2RuPressureAvg,
                Tcm2280ProcessDatas::getTcm3RuPressureAvg,
                Tcm2280ProcessDatas::getTcm4RuPressureAvg,
                Tcm2280ProcessDatas::getTcm5RuPressureAvg
        );

        List<Tcm2280ProcessDatas> calList = list(w);
        for (Tcm2280ProcessDatas cal : calList) {
            obj.getExtIdArr().add(cal.getExtid());
            obj.getCredateArr().add(cal.getEndtime());
            obj.getTcmProcessSpeedArr().add(cal.getTcmProcessSpeedAvg());
            obj.getTcmTank1TempArr().add(cal.getTcmTank1TempAvg());
            obj.getTcmTank2TempArr().add(cal.getTcmTank2TempAvg());
            obj.getTcmTank3TempArr().add(cal.getTcmTank3TempAvg());
            obj.getAcid1().add(cal.getAcid1());
            obj.getFeContent1().add(cal.getFeContent1());
            obj.getAcid3().add(cal.getAcid3());
            obj.getFeContent3().add(cal.getFeContent3());
            obj.getTcmRinsePhArr().add(cal.getTcmRinsePhAvg());
            obj.getTcmDryTempArr().add(cal.getTcmDryTempAvg());
            obj.getRollSpeedArr().add(cal.getRollSpeedAvg());
            obj.getTcmTank1EmuTempArr().add(cal.getTcmTank1EmuTempAvg());
            obj.getTcmTank2EmuTempArr().add(cal.getTcmTank2EmuTempAvg());
            obj.getTcmTank3EmuTempArr().add(cal.getTcmTank3EmuTempAvg());
            obj.getTcm1RuPressureArr().add(cal.getTcm1RuPressureAvg());
            obj.getTcm2RuPressureArr().add(cal.getTcm2RuPressureAvg());
            obj.getTcm3RuPressureArr().add(cal.getTcm3RuPressureAvg());
            obj.getTcm4RuPressureArr().add(cal.getTcm4RuPressureAvg());
            obj.getTcm5RuPressureArr().add(cal.getTcm5RuPressureAvg());
        }

        return ResultData.ResultDataSuccess(obj);
    }

    @Override
    @Transactional
    public ResultData<String> transData(String beginTime, String endTime) {
        Map<String, Tcm2280Base> m = new HashMap<>();
        List<Tcm2280Base> bar = mapper.selectTcm2280Data(beginTime, endTime);
        //订单最终用户可能有重复，按订单编号最大的计
        for (Tcm2280Base tb : bar) {
            m.put(tb.getExtid(), tb);
        }
        List<Tcm2280Base> list = new ArrayList<>();
        for (Map.Entry<String, Tcm2280Base> entry : m.entrySet()) {
            list.add(entry.getValue());
        }

        String[] arr = new String[]{
                "tcm_process_speed_avg", "tcm_tank1_temp_avg", "tcm_tank2_temp_avg",
                "tcm_tank3_temp_avg", "tcm_rinse_ph_avg", "tcm_dry_temp_avg",
                "tcm_tank1_emu_temp_avg", "tcm_tank2_emu_temp_avg", "tcm_tank3_emu_temp_avg",
                "tcm_1#_ru_pressure_avg", "tcm_2#_ru_pressure_avg", "tcm_3#_ru_pressure_avg",
                "tcm_4#_ru_pressure_avg", "tcm_5#_ru_pressure_avg", "tcm_s5_strip_speed_act_avg"};

        //获取验证参数字典
        Map<String, TTechTcm2280Param> paramMap = new HashMap<>();
        List<TTechTcm2280Param> paramList = paramService.list();
        for (TTechTcm2280Param p : paramList) {
            String zhibiao = "";
            if (StringUtils.isNotEmpty(p.getZhibiao())) {
                zhibiao = p.getZhibiao();
            }
            String brand = "";
            if (StringUtils.isNotEmpty(p.getBrand())) {
                brand = p.getBrand();
            }
            String comp = "";
            if (StringUtils.isNotEmpty(p.getCompany())) {
                comp = p.getCompany();
            }
            paramMap.put(p.getProNo() + p.getSurface() + "_" + zhibiao + "_" + brand + "_" + comp, p);
        }

        //查询铁离子和游离酸数据
        LambdaQueryWrapper<TSolutionPdo> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(TSolutionPdo::getCreateTime).last(" fetch first 1 rows only");
        TSolutionPdo solu = solutionService.list(wrapper).get(0);
        BigDecimal acid1 = solu.getHclValue1();
        BigDecimal acid3 = solu.getHclValue3();
        BigDecimal fe1 = solu.getFe2Value1();
        BigDecimal fe3 = solu.getFe2Value3();

        List<Tcm2280ProcessDatas> tcmList = new ArrayList<>();
        for (Tcm2280Base item : list) {
            Tcm2280ProcessDatas obj = new Tcm2280ProcessDatas();
            obj.setEntid(item.getEntid());
            obj.setExtid(item.getExtid());
            obj.setCrew(item.getCrew());
            obj.setStartime(item.getProdStartTime());
            obj.setEndtime(item.getProdEndTime());
            obj.setToc(item.getCreateTime());
            obj.setSteelgrade(item.getSteelgrade());
            obj.setInMatWidth(item.getInMatWidth());
            obj.setInMatThk(item.getInMatThick());
            obj.setSurfaceAccuClassCode(item.getCoatingType());
            obj.setSteelclass(item.getSteelclass());
            obj.setSaleOrderNo(item.getSaleOrderNo());
            obj.setFinCustCname(item.getFinCustCname());
            obj.setFeContent1(fe1);
            obj.setFeContent3(fe3);
            obj.setAcid1(acid1);
            obj.setAcid3(acid3);

            //从MongoDB中读取轧卷对应的指标
            Query query = new Query();
            query.addCriteria(Criteria.where("flag").is(1));
            for (int i = 0; i < arr.length; i++) {
                query.fields().include("trailValues." + arr[i]);
            }
            query.addCriteria(Criteria.where("extid").is(item.getExtid()));

            Map<String, Double> map;
            //从MongoDB中加载数据
            JDataSavemong2280_1 mongoData = mongoTemplate.findOne(query, JDataSavemong2280_1.class);
            if (mongoData == null) {
                map = new HashMap<>();
                log.warn("卷号{}，获取MongoDB数据失败", item.getExtid());
            } else {
                map = mongoData.getTrailValues();
            }

            obj.setTcmProcessSpeedAvg(readValue(map, arr[0]));
            obj.setTcmTank1TempAvg(readValue(map, arr[1]));
            obj.setTcmTank2TempAvg(readValue(map, arr[2]));
            obj.setTcmTank3TempAvg(readValue(map, arr[3]));
            obj.setTcmRinsePhAvg(readValue(map, arr[4]));
            obj.setTcmDryTempAvg(readValue(map, arr[5]));
            obj.setTcmTank1EmuTempAvg(readValue(map, arr[6]));
            obj.setTcmTank2EmuTempAvg(readValue(map, arr[7]));
            obj.setTcmTank3EmuTempAvg(readValue(map, arr[8]));
            obj.setTcm1RuPressureAvg(readValue(map, arr[9]));
            obj.setTcm2RuPressureAvg(readValue(map, arr[10]));
            obj.setTcm3RuPressureAvg(readValue(map, arr[11]));
            obj.setTcm4RuPressureAvg(readValue(map, arr[12]));
            obj.setTcm5RuPressureAvg(readValue(map, arr[13]));
            obj.setRollSpeedAvg(readValue(map, arr[14]));

            paramService.checkParam(paramMap, obj);

            tcmList.add(obj);
        }
        saveBatch(tcmList);
        return ResultData.ResultDataSuccess("success");
    }

    @Override
    public List<TechListDtoRes> load2280ProcessData(TechChartDtoReq tc) {
        List<TechListDtoRes> list = new ArrayList<>();

        LambdaQueryWrapper<Tcm2280ProcessDatas> w = buildTcm2280Wrapper(tc);
        //查询出口卷号
        w.select(Tcm2280ProcessDatas::getExtid);
        List<Tcm2280ProcessDatas> dbList = list(w);
        for (int i = 0; i < dbList.size(); i++) {
            Tcm2280ProcessDatas obj = dbList.get(i);
            TechListDtoRes res = new TechListDtoRes();
            res.setSeqno(i + 1);
            res.setVolumeNo(obj.getExtid());
            list.add(res);
        }
        return list;
    }

    @Override
    public ResultData<String> rollInfo(TechChartDtoReq tc) {
        Date d = new Date();
        String d1 = DateUtil.getBeforeHourStart(d);
        String d2 = DateUtil.getDateToString(d, "yyyyMMddHH");
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        int h = cal.get(Calendar.HOUR_OF_DAY);

        LambdaQueryWrapper<Tcm2280ProcessDatas> wrapper = new LambdaQueryWrapper();
        wrapper.select(Tcm2280ProcessDatas::getTcmProcessSpeedJudge,
                Tcm2280ProcessDatas::getTcmTank1TempJudge,
                Tcm2280ProcessDatas::getTcmTank2TempJudge,
                Tcm2280ProcessDatas::getTcmTank3TempJudge,
                Tcm2280ProcessDatas::getTcmRinsePhJudge,
                Tcm2280ProcessDatas::getTcmDryTempJudge,
                Tcm2280ProcessDatas::getRollSpeedJudge,
                Tcm2280ProcessDatas::getTcmTank1EmuTempJudge,
                Tcm2280ProcessDatas::getTcmTank2EmuTempJudge,
                Tcm2280ProcessDatas::getTcmTank3EmuTempJudge,
                Tcm2280ProcessDatas::getTcm1RuPressureJudge,
                Tcm2280ProcessDatas::getTcm2RuPressureJudge,
                Tcm2280ProcessDatas::getTcm3RuPressureJudge,
                Tcm2280ProcessDatas::getTcm4RuPressureJudge,
                Tcm2280ProcessDatas::getTcm5RuPressureJudge,
                Tcm2280ProcessDatas::getAcidJudge1,
                Tcm2280ProcessDatas::getFeContentJudge1,
                Tcm2280ProcessDatas::getAcidJudge3,
                Tcm2280ProcessDatas::getFeContentJudge3,
                Tcm2280ProcessDatas::getFeContentJudge,
                Tcm2280ProcessDatas::getImpureOilJudge,
                Tcm2280ProcessDatas::getBandFslvJudge
        ).ge(Tcm2280ProcessDatas::getEndtime, d1).le(Tcm2280ProcessDatas::getEndtime, d2);
        List<Tcm2280ProcessDatas> dbList = list(wrapper);
        String[] titleArr = new String[]{
                "工艺段速度", "1#酸槽温度", "2#酸槽温度", "3#酸槽温度", "漂洗槽PH值",
                "烘干机温度", "轧制速度", "乳化液1#箱温度", "乳化液2#箱温度", "乳化液3#箱温度",
                "1#乳化液喷射梁压力", "2#乳化液喷射梁压力", "3#乳化液喷射梁压力", "4#乳化液喷射梁压力", "5#乳化液喷射梁压力",
                "1#游离酸浓度", "1#铁离子浓度", "3#游离酸浓度", "3#铁离子浓度", "铁含量",
                "杂油含量", "带钢表面反射率"};
        int[] countArr = new int[titleArr.length];
        for (Tcm2280ProcessDatas data : dbList) {
            if (data.getTcmProcessSpeedJudge() != null && data.getTcmProcessSpeedJudge() == 1) countArr[0]++;
            if (data.getTcmTank1TempJudge() != null && data.getTcmTank1TempJudge() == 1) countArr[1]++;
            if (data.getTcmTank2TempJudge() != null && data.getTcmTank2TempJudge() == 1) countArr[2]++;
            if (data.getTcmTank3TempJudge() != null && data.getTcmTank3TempJudge() == 1) countArr[3]++;
            if (data.getTcmRinsePhJudge() != null && data.getTcmRinsePhJudge() == 1) countArr[4]++;

            if (data.getTcmDryTempJudge() != null && data.getTcmDryTempJudge() == 1) countArr[5]++;
            if (data.getRollSpeedJudge() != null && data.getRollSpeedJudge() == 1) countArr[6]++;
            if (data.getTcmTank1EmuTempJudge() != null && data.getTcmTank1EmuTempJudge() == 1) countArr[7]++;
            if (data.getTcmTank2EmuTempJudge() != null && data.getTcmTank2EmuTempJudge() == 1) countArr[8]++;
            if (data.getTcmTank3EmuTempJudge() != null && data.getTcmTank3EmuTempJudge() == 1) countArr[9]++;

            if (data.getTcm1RuPressureJudge() != null && data.getTcm1RuPressureJudge() == 1) countArr[10]++;
            if (data.getTcm2RuPressureJudge() != null && data.getTcm2RuPressureJudge() == 1) countArr[11]++;
            if (data.getTcm3RuPressureJudge() != null && data.getTcm3RuPressureJudge() == 1) countArr[12]++;
            if (data.getTcm4RuPressureJudge() != null && data.getTcm4RuPressureJudge() == 1) countArr[13]++;
            if (data.getTcm5RuPressureJudge() != null && data.getTcm5RuPressureJudge() == 1) countArr[14]++;

            if (data.getAcidJudge1() != null && data.getAcidJudge1() == 1) countArr[15]++;
            if (data.getFeContentJudge1() != null && data.getFeContentJudge1() == 1) countArr[16]++;
            if (data.getAcidJudge3() != null && data.getAcidJudge3() == 1) countArr[17]++;
            if (data.getFeContentJudge3() != null && data.getFeContentJudge3() == 1) countArr[18]++;
            if (data.getFeContentJudge() != null && data.getFeContentJudge() == 1) countArr[19]++;

            if (data.getImpureOilJudge() != null && data.getImpureOilJudge() == 1) countArr[20]++;
            if (data.getBandFslvJudge() != null && data.getBandFslvJudge() == 1) countArr[21]++;
        }
        List<RollInfo> list = new ArrayList<>();
        for (int i = 0; i < titleArr.length; i++) {
            list.add(new RollInfo(titleArr[i], countArr[i]));
        }
        Collections.sort(list);

        String res = "";
        StringBuffer sbf = new StringBuffer();
        sbf.append(h - 1).append(":00-").append(h).append(":00").append(" 总生产卷数:" + dbList.size());
        sbf.append(list.size() == 0 ? "。" : "，");
        for (int i = 0; i < list.size(); i++) {
            RollInfo ri = list.get(i);
            if (ri.getCount() == 0) {
                res = sbf.substring(0, sbf.length() - 1) + "。";
                break;
            }
            sbf.append(ri.getTitle()).append("超限:").append(ri.getCount());
            sbf.append(i < list.size() - 1 ? "，" : "。");
        }
        return ResultData.ResultDataSuccess(res.length() > 0 ? res : sbf.toString());
    }


    @Override
    public ResultData<TechFitRes> techFitRate(String startDate, String endDate) {
        List<TechFitRes> list = new ArrayList<>();

        List<RepTechFit> repList = techFitService.selectCrewThisMonth(startDate, endDate);
        Collections.sort(repList);
        for (RepTechFit rep : repList) {
            String crew = CommonConstant.crewMap.get(rep.getCrew()) + "班";
            TechFitRes t1 = new TechFitRes();
            t1.setCrew(crew);
            t1.setArea("入口段");
            t1.setTotal(rep.getZongjuans());
            t1.setTechCount(1);
            int rukouSuc = rep.getZongjuans() - rep.getRukoud();
            t1.setSucCount(rukouSuc);
            t1.setRate(BigDecimal.valueOf(rukouSuc).multiply(BigDecimal.valueOf(100))
                    .divide(BigDecimal.valueOf(rep.getZongjuans()), 2, RoundingMode.HALF_UP));
            list.add(t1);

            TechFitRes t2 = new TechFitRes();
            t2.setCrew(crew);
            t2.setArea("酸洗段");
            t2.setTotal(rep.getZongjuans());
            t2.setTechCount(10);
            int suanxiSuc = rep.getZongjuans() - rep.getSuanxi();
            t2.setSucCount(suanxiSuc);
            t2.setRate(BigDecimal.valueOf(suanxiSuc).multiply(BigDecimal.valueOf(100))
                    .divide(BigDecimal.valueOf(rep.getZongjuans()), 2, RoundingMode.HALF_UP));
            list.add(t2);

            TechFitRes t3 = new TechFitRes();
            t3.setCrew(crew);
            t3.setArea("轧制段");
            t3.setTotal(rep.getZongjuans());
            t3.setTechCount(11);
            int zhazhiSuc = rep.getZongjuans() - rep.getZhazhi();
            t3.setSucCount(zhazhiSuc);
            t3.setRate(BigDecimal.valueOf(zhazhiSuc).multiply(BigDecimal.valueOf(100))
                    .divide(BigDecimal.valueOf(rep.getZongjuans()), 2, RoundingMode.HALF_UP));
            list.add(t3);
        }

        return ResultData.ResultDataSuccess(list, list.size());
    }

    @Override
    public ResultData<TechOverViewRes> techOverview(String startDate, String endDate) {
        List<TechOverViewRes> list = new ArrayList<>();
        String[] techNameArr = new String[]{
                "拉矫机延伸率", "酸洗速度", "1#酸槽温度", "2#酸槽温度", "3#酸槽温度",
                "1#游离酸浓度", "1#铁离子浓度", "3#游离酸浓度", "3#铁离子浓度", "4#漂洗槽PH值",
                "烘干机温度", "轧制速度", "乳化液1#箱温度", "乳化液2#箱温度", "乳化液3#箱温度",
                "杂油", "1#乳化液喷射梁压力", "2#乳化液喷射梁压力", "3#乳化液喷射梁压力", "4#乳化液喷射梁压力",
                "5#乳化液喷射梁压力", "带钢表面反射率"
        };
        String[] lowArr = new String[]{"0%", "", "", "", "", "40g/L", "120g/L", "160g/L", "30g/L", "",
                "126℃", "", "50", "50", "50", "", "", "", "", "", "", ""};
        String[] hignArr = new String[]{"4%", "", "", "", "", "", "", "", "", "",
                "130℃", "", "60", "60", "60", "", "", "", "", "", "", ""};
        String[] getArr = new String[]{
                "tcmTlElongCount", "tcmProcessSpeedCount", "tcmTank1TempCount", "tcmTank2TempCount", "tcmTank3TempCount",
                "acid1Count", "feContent1Count", "acid3Count", "feContent3Count", "tcmRinsePhCount",
                "tcmDryTempCount", "rollSpeedCount", "tcmTank1EmuTempCount", "tcmTank2EmuTempCount", "tcmTank3EmuTempCount",
                "impureOilCount", "tcm1RuPressureCount", "tcm2RuPressureCount", "tcm3RuPressureCount", "tcm4RuPressureCount",
                "tcm5RuPressureCount", "bandFslvCount"
        };
        RepTechFit techFit = techFitService.selectThisMonth(startDate, endDate);
        for (int i = 0; i < techNameArr.length; i++) {
            TechOverViewRes rep = new TechOverViewRes();
            if (i == 0) rep.setArea("入口段");
            else if (i <= 10) rep.setArea("酸洗段");
            else rep.setArea("轧机段");

            rep.setTechName(techNameArr[i]);
            rep.setLowLevel(lowArr[i]);
            rep.setHighLevel(hignArr[i]);

            rep.setTotalCount(techFit.getZongjuans());
            try {
                String method = "get" + getArr[i].substring(0, 1).toUpperCase() + getArr[i].substring(1);
                rep.setFailCount((int) techFit.getClass().getMethod(method).invoke(techFit));
                BigDecimal rate = BigDecimal.valueOf(rep.getFailCount()).multiply(BigDecimal.valueOf(100))
                        .divide(BigDecimal.valueOf(rep.getTotalCount()), 2, RoundingMode.HALF_UP);
                rep.setRate(rate);
            } catch (Exception e) {
                e.printStackTrace();
            }
            list.add(rep);
        }

        return ResultData.ResultDataSuccess(list, list.size());
    }

    private LambdaQueryWrapper<Tcm2280ProcessDatas> buildTcm2280Wrapper(TechChartDtoReq tc) {
        Date d1 = DateUtil.getStringToDate("yyyy-MM-dd HH:mm:ss", tc.getStartDate());
        Date d2 = DateUtil.getStringToDate("yyyy-MM-dd HH:mm:ss", tc.getEndDate());
        String beginTime = "";
        String endTime = "";
        LambdaQueryWrapper<Tcm2280ProcessDatas> w = new LambdaQueryWrapper<>();
        //开始时间
        if (StringUtils.isNotEmpty(tc.getStartDate())) {
            beginTime = DateUtil.getDateToString(d1, "yyyyMMddHHmmss");
            w.ge(Tcm2280ProcessDatas::getEndtime, beginTime);
        } else {
            //最近1天
            Calendar cal = Calendar.getInstance();
            cal.setTime(d1);
            cal.add(Calendar.DAY_OF_MONTH, -1);
            beginTime = DateUtil.getDateToString(cal.getTime(), "yyyyMMddHHmmss");
            w.ge(Tcm2280ProcessDatas::getEndtime, beginTime);
        }
        //结束时间
        if (StringUtils.isNotEmpty(tc.getEndDate())) {
            endTime = DateUtil.getDateToString(d2, "yyyyMMddHHmmss");
            w.le(Tcm2280ProcessDatas::getEndtime, endTime);
        }
        //钢种
        if (StringUtils.isNotEmpty(tc.getSteelGrade())) {
            w.eq(Tcm2280ProcessDatas::getSteelgrade, tc.getSteelGrade());
        }
        //入口宽度
        if (tc.getEntWidthStart() != null) {
            w.ge(Tcm2280ProcessDatas::getInMatWidth, tc.getEntWidthStart());
        }
        if (tc.getEntWidthEnd() != null) {
            w.le(Tcm2280ProcessDatas::getInMatWidth, tc.getEntWidthEnd());
        }
        //入口厚度
        if (tc.getEntThickStart() != null) {
            w.ge(Tcm2280ProcessDatas::getInMatThk, tc.getEntThickStart());
        }
        if (tc.getEntThickEnd() != null) {
            w.le(Tcm2280ProcessDatas::getInMatThk, tc.getEntThickEnd());
        }
        if (StringUtils.isNotEmpty(tc.getCrew())) {
            w.eq(Tcm2280ProcessDatas::getCrew, tc.getCrew());
        }
        w.orderByAsc(Tcm2280ProcessDatas::getEndtime);
        return w;
    }

    private BigDecimal readValue(Map<String, Double> map, String key) {
        if (map.get(key) != null) {
            return BigDecimal.valueOf(map.get(key));
        }
        log.warn("{} 变量数据不存在", key);
        return null;
    }
}
